var url="ws://127.0.0.1:3417";  

var socket
var videoCanvas = null
var camera = 0
var strcmd = {
    command:10001,
    code:1,
    camidx:camera,
    autograp:0,
    autocrop:1,
    videoCanvas_width:null,
    videoCanvas_height:null,
    videoformat:null,
    bSelected:0,
    angle:0,
    width:0,
    height:0,
}
var objheight=0
var objwidth=0
var photos
var ctx=null
var videosrc = ''
var loading = false

function resetparameter(){
    camera = 0
    strcmd= {
        command:10001,
        code:1,
        camidx:camera,
        autograp:0,
        autocrop:1,
        videoCanvas_width:null,
        videoCanvas_height:null,
        videoformat:null,
        bSelected:0,
        angle:0,
        width:0,
        height:0,
    }
}
function createWebSocket (dom){
    socket= new WebSocket(url);
    socket.onmessage = (msg)=>{
        var data = msg.data;
        if(typeof data === "string"){
            var obj = JSON.parse(data);
            if(obj.command==20001){
                // videosrc = "data:image/jpeg;base64," +  obj.video
                var Videoimg = new Image();  
                Videoimg.src = "data:image/jpeg;base64," +  obj.video;	
                Videoimg.onload = function(){			
                    if(camera==1){
                        ctx.drawImage(this,0,0,strcmd.videoCanvas_width*(objheight/strcmd.videoCanvas_height) ,objheight);
                    }else{
                        ctx.drawImage(this,0,0,strcmd.videoCanvas_width ,strcmd.videoCanvas_height);
                    }
                }             
                
            }
            if(obj.command==20003){            
                photos = "data:image/jpeg;base64," +  obj.pic;	
            }
        }
    }
    socket.onerror = function(error){
        if(!error.wasClean){
            createWebSocket()
        }
    
    
    };
    socket.onopen = function() { 
    }
    socket.onclose = function (e){
        // console.log(123);
        
    }
    let time = setInterval(()=>{
        if(socket.readyState==1){
            loading=false
            clearInterval(time)
            StartVideo(dom)
        }else{
            loading=true
        }
    },200)
}

function StartVideo (dom){
    videoCanvas = dom
    ctx=videoCanvas.getContext("2d");
    var videoformat=2;   //0: default 1:YUY2 2:mjpg
    strcmd.videoCanvas_height = dom.height
    strcmd.videoCanvas_width = dom.width
    strcmd.videoformat = videoformat
    strcmd.camidx = camera
    socket.send(JSON.stringify(strcmd))
}
function StopVideo(url)
{
    var strcmd = {
        command:10001,
        code:0,
    }
    if(socket.readyState==1){
        socket.send(JSON.stringify(strcmd))
    }
}
function changeCamera(width,height){
    objheight = Number(height.split('px')[0])
    objwidth = Number(width.split('px')[0])
    // strcmd.videoCanvas_height = Number(height.split('px')[0])
    // strcmd.videoCanvas_width = Number(width.split('px')[0])
    // strcmd.height = Number(height.split('px')[0])
    // strcmd.width = Number(height.split('px')[0])
    // console.log(strcmd.videoCanvas_height,'777777');
    if(camera==1){
        camera = 0
    }else{
        camera = 1
    }
    // strcmd.camidx = camera
    socket.send(JSON.stringify({
        command:10001,
        code:1,
        camidx:camera,
        autograp:0,
        autocrop:0,
        videoCanvas_width:videoCanvas.height,
        videoCanvas_height:videoCanvas.width,
    }))
} 
 
 
function takePhotos (){
    socket.send(JSON.stringify({command:10003,quality:80,bShow:1,angle:0}))
}


function getcamera(){
    return camera
}
function big(){
    strcmd.command = 10024
    socket.send(JSON.stringify(strcmd))
}
function small(){
    strcmd.command = 10025
    socket.send(JSON.stringify(strcmd))
}
function rotate(num){
    // strcmd.command = 10009
    // strcmd.angle = num
    socket.send(JSON.stringify({command:10009,angle:num}))
}
function Fnsplit(num){
    // strcmd.command = 10009
    // strcmd.angle = num
    socket.send(JSON.stringify({command:10007,bEnable:num}))
}
function closeWebSocket(){
    if(socket.readyState==1){
        socket.close()
    }
}
function getphtots(){
    return photos
}
function getvideosrc(){
    return videosrc
}
function getloading(){
    return loading
}
export default {
    StartVideo,  // 开启摄像头
    StopVideo,  //关闭摄像头
    changeCamera, //摄像头切换
    takePhotos , //拍照
    big,  //放大
    small,  //缩小
    rotate, //旋转
    Fnsplit,
    closeWebSocket,
    getcamera,//摄像头
    getphtots,
    resetparameter,
    createWebSocket,
    getvideosrc,
    getloading
}